<?php
namespace app\index\controller;
use addons\comments\model\CommentsPostModel;
use app\admin\controller\order\service\OrderDrive;
use app\admin\model\steward\Apps;
use app\admin\model\system\Debug;
use app\admin\model\user\User;
use app\post\model\Category;
use app\post\model\Post;
use EasyWeChat\Factory;
use think\facade\Log;
use think\Request;

class Index extends Base
{
    public function index()
    {
        //分类tab文章
        $cateList =  Category::limit(6)
            ->select();
        foreach ($cateList as $k=>$v)
        {
            $cateList[$k]['article'] = Post::where("b.category_id",$v['id'])
                ->alias("a")
                ->join("cp_portal_category_post b","a.id=b.post_id")
                ->field("a.id,a.post_title,a.thumbnail")
                ->limit(6)
                ->select();
        }
        $appCate = \app\admin\model\steward\Category::field("id,name,alias")->select();
        foreach ($appCate as $kk=>$v)
        {
            //分类tab应用
            $appCate[$kk]['app_list'] = Apps::field("a.id,a.app_name,a.app_photo")
                ->alias("a")
                ->join("cp_apps_category_app b","a.id=b.post_id")
                ->order("update_time desc")
                ->where("b.category_id",$v['id'])
                ->limit(9)
                ->select();
        }

        $hotApp = Apps::order("app_install desc")
            ->limit(10)
            ->select();

        $this->assign([
            "cateList"   => $cateList,
            "cateAppList"=> $appCate,
            "hotApp"     => $hotApp
        ]);

        return $this->fetch();
    }

    public function debug(Request $request)
    {
        if($request->isPost())
        {
            if(!_get_current_user_id()) $this->error("请登录之后操作");
            $input = $request->post();
            $res = (new Debug())->create($input);
            if($res)
            {
                $this->success("感谢您的反馈！我们会在3个工作日内给您答复",url('index/index/index'));
            }else{
                $this->error("反馈失败！请稍后再试");
            }
        }
        return  view();
    }

    public function notice()
    {
        return view();
    }

    public function user_jianjie($user)
    {
        $user = User::where("id",$user)->find();
        $user['ruzu'] = Sec2Time(strtotime($user['create_time']));

        return view("",[
            "user" => $user,
        ]);
    }

    public function user_comments($user)
    {
        $user = User::where("id",$user)->find();
        $user['ruzu'] = Sec2Time(strtotime($user['create_time']));
        $commentList = CommentsPostModel::where("from_uid",$user['id'])->paginate(10);
        foreach ($commentList as $k=>$v)
        {
            //$commentList[$k]['from_user'] = User::where("id",$v['from_uid'])->field("id,nickname,username,avatar")->find();

            $commentList[$k]['to_user']   = User::where("id",$v['to_uid'])->field("id,nickname,username,avatar")->find();
            if($v['topic_type']==1)
            {
                $commentList[$k]['article'] = Post::where("id",$v['topic_id'])->field("id,post_title")->find();
            }
        }

        return view("",[
            "user" => $user,
            "commentList" => $commentList
        ]);
    }


    public function pay_test(){
        (new OrderDrive())->notice();
    }
}
